package com.dux.commons.configuration.transformers;

import java.lang.reflect.Field;

import com.dux.commons.configuration.PropertyTransformer;
import com.dux.commons.configuration.TransformationException;


/**
 * 字符串转换成枚举类型
 * @author 杜祥
 * @create 2013-6-29
 */
public class EnumTransformer implements PropertyTransformer<Enum<?>>
{
	public static final EnumTransformer	SHARED_INSTANCE	= new EnumTransformer();

	@Override
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Enum<?> transform(String value, Field field) throws TransformationException
	{
		Class<? extends Enum> clazz = (Class<? extends Enum>) field.getType();

		try
		{
			return Enum.valueOf(clazz, value);
		}
		catch(Exception e)
		{
			throw new TransformationException(e);
		}
	}
}
